<template>
	<view class="pdf-detail">
		<view class="image-show">
			<image style="width: 100%;" :src="resourceMsg.preview"></image>
		</view>
		<view v-if="!filePath" class="pdf-loading">
			<u-loading size="50"  mode="flower"></u-loading>
			<text>正在加载pdf</text>
		</view>
		<view v-else class="pdf-loading">
			<u-button type="primary" size="medium" @click="open">查看文档</u-button>
		</view>
	</view>
</template>

<script>
	export default {
		resourceMsg:{
			type:Object,
			default(){
				return {}
			}
		},
		data(){
			return {
				filePath:'',
			}
		},
		mounted() {
			this.downPdf()
		},
		methods:{
			downPdf(){
				let testUrl = this.resourceMsg.fileInfos[0].fileName
				let pdfPath = uni.getStorageSync('pdfPath') || []
				console.log(pdfPath)
				let val = pdfPath.find((el)=>{
					return el.name == testUrl
				})
				if(val){
					this.filePath = val.filePath
					uni.openDocument({
					  filePath: val.filePath,
					  success: function (res) {
						console.log('打开文档成功');
					  }
					});
				}else{
					uni.downloadFile({
					  url: testUrl,
					  success: function (res) {
						var filePath = res.tempFilePath;
						this.filePath = filePath
						uni.openDocument({
						  filePath: filePath,
						  success: function (res) {
							console.log('打开文档成功');
							pdfPath.push({
								name:testUrl,
								filePath:filePath
							})
							
							uni.setStorageSync('pdfPath',pdfPath)
						  }
						});
					  }
					});
				}
				
			},
			open(){
				uni.openDocument({
				  filePath: this.filePath,
				  success: function (res) {
					console.log('打开文档成功');
				  }
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.pdf-detail{
		.image-show{
			
		}
		.pdf-loading{
			text-align: center;
		}
	}
</style>
